package cn.sfa.service.routine;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import microsoft.exchange.webservices.data.Appointment;
import microsoft.exchange.webservices.data.ServiceLocalException;
import microsoft.exchange.webservices.data.StringList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.sfa.common.calendar.CalendarHelper;
import cn.sfa.common.calendar.SolarTerm;
import cn.sfa.common.utils.CommonUtil;
import cn.sfa.entity.routine.SaleCalendar;
import cn.sfa.entity.routine.SaleMessagePic;
import cn.sfa.entity.routine.SalePrivateAffair;
/**
 * 
* 类名称：CalendarService   
* 类描述：行事日历服务类
* 创建人：lenovo   
* 创建时间：2012-2-20 上午11:06:30   
* 修改人：lenovo   
* 修改时间：2012-2-20 上午11:06:30   
* 修改备注：   
* @version
 */
@Service
@Transactional
public class CalendarService {
	
	Logger log = LoggerFactory.getLogger(CalendarService.class);

	@Autowired
	NamedParameterJdbcTemplate jdbcTemplate;
	

	/*
	 * 获取日历
	 */
    public  Map getCalendar(int y , int m,String username,String pwd) {

		ExChangeCalendarService ser = new ExChangeCalendarService();
    	Map map = new HashMap();
    	 
        
        Calendar thisMonth = Calendar.getInstance();

        thisMonth.set(y, m-1, 1); 
 
        int maxIndex = thisMonth.getActualMaximum(Calendar.DAY_OF_MONTH);
 
        
        List<List<SaleCalendar>> calendarList = new ArrayList<List<SaleCalendar>>();
        List<SaleCalendar> weekList =null; 
         
        Map calendar =  ser.getCalendar(y, m,username,pwd);    
        int dayIndex = 1;
        boolean tmpTag = true;
        //获取节气
        SolarTerm term = new SolarTerm();
        String solarTerm = "";
        for(int i = 0;tmpTag;i++){ 
        	weekList = new ArrayList<SaleCalendar>();
        	
        	for(int j=0;j<7;j++){
				thisMonth.set(y, m-1, dayIndex);
				SaleCalendar cal = null;
				if (j == thisMonth.get(Calendar.DAY_OF_WEEK)-1&&tmpTag) {
	        		cal = new SaleCalendar();
	        		cal.setYear(String.valueOf(y));
	        		cal.setMonth(String.valueOf(m));
	        		cal.setDay(String.valueOf(dayIndex));
	        		GregorianCalendar selDate = new GregorianCalendar(y,m-1,dayIndex); 
	        		CalendarHelper calHelper = new CalendarHelper(selDate);
	        		cal.setNongli(calHelper.toShortString());
	        		solarTerm = term.getSoralTerm(y, m, dayIndex);
	        		if(!CommonUtil.isEmpty(solarTerm)){
	        			cal.setNongli(solarTerm);
	        		}
	        		List<Appointment> appList = null;
	        		if(calendar.keySet().contains(dayIndex)){
	        		  appList =	(ArrayList<Appointment>)calendar.get(dayIndex);
	        		}else{
	        			appList = new ArrayList<Appointment>();
	        		}

	        		List<Appointment> newAppList = new ArrayList<Appointment>();
	        		StringList categories = new StringList();
	        		categories.add("其他");
	        		if(appList.size()>0){ 
	        		for(Appointment app : appList){  
	        			
								try {
									if(app.getCategories().getSize()==0){ 
											app.setCategories(categories); 
									 }
									newAppList.add(app);
								} catch (ServiceLocalException e) { 
									e.printStackTrace();
								} catch (Exception e) { 
									e.printStackTrace();
								}
						 
	        			 
	        		}
	        		}
	        		
	        		cal.setSaleAppointment(newAppList); 
	        		cal.setMore(appList.size()>2);
	        		dayIndex ++; 
	        		
				}           		
        		weekList.add(cal);

        		if(dayIndex>maxIndex){
        			tmpTag=false;
        		}
        	}
        	calendarList.add(weekList);
        }   
        map.put("list", calendarList);
        
        return map;

    }    
	
	   
	
	//获取消息类型对应的图片路径
	public Map getMessagePic(){
		List<SaleMessagePic> picList = null; 
		
		String sql  = "select messagename,messagepictureurl from salemessagebase";
		picList =jdbcTemplate.query(sql, new HashMap(), new BeanPropertyRowMapper(SaleMessagePic.class));

		Map urlMap = new HashMap(); 
		for(SaleMessagePic url : picList){
			urlMap.put(url.getMessagename(),url.getMessagepictureurl()); 
		}
		urlMap.put("其他", "000.gif"); 
		return urlMap; 
	}
	
}
